package com.zhonglian.elisa.customer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

import com.zhonglian.elisa.utils.LogUtil;


/**
 * Created by yx on 2020/3/25.
 * 屏蔽ScrollView滑动操作
 */

public class MyScrollview extends ScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;
    private boolean mIsScroll = false;//false子控件获得滑动事件，trueScrollView获得焦点

    private ScrollViewListener scrollViewListener = null;

    public MyScrollview(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

    public interface ScrollViewListener {
        void onScrollChanged(MyScrollview scrollView, int x, int y, int oldx, int oldy);
    }

    /**
     * 控制是否让子控件获得滑动事件
     */
    public void setIsScroll(boolean isScroll) {
        this.mIsScroll = isScroll;
    }

    //scrollview拦截上下滑动事件
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getRawX();
                downY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) ev.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    /**
                     * 上下滑动时返回false，ScrollView不处理这次事件，
                     * 让子控件中的TouchEvent去处理,返回true本次Scroll获得上下滑动权利
                     */
                    return mIsScroll;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

}